package yl.hs.bmipfull.controllers.construction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.salemanage.ISaleOrderService;
import yl.hs.bmipfull.utils.datastate.SaleOrderState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;

/*
 * ConstructionSaleOrderController 工地销售订单接口
 * @category 销售管理
 * @author 谢景广
 * @date 2022-04-21
 */
@RequestMapping("/construction/saleOrders")
@Api(value = "工地系统", tags = "工地系统——销售订单")
@RestController
public class ConstructionSaleOrderController extends ControllerBase {

    @Autowired
    ISaleOrderService orderService;

    @PostMapping("query/{rowNum}/{page}")
    @ApiOperation(value = "查询所有远程下单信息", notes = "分页查询所有远程下单信息" )
    public WebResult queryOrderList(@PathVariable("rowNum")int rowNum, @PathVariable("page")int page, @RequestBody PageQuery query){
        try {
            var result = orderService.queryContractOrders(query,rowNum,page);
            return success("OK", result);
        }
        catch (Exception ee){
            return  error(ee);
        }
    }
    @RequestMapping(value = "count",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有远程下单信息", notes = "分页查询所有远程下单信息" )
    public WebResult querycount(){
        try {
            var result = orderService.queryContractOrders(new PageQuery(),10,1);
            return success("OK", result.getRecords());
        }
        catch (Exception ee){
            return  error(ee);
        }
    }

    @PostMapping("save")
    @ApiOperation(value = "保存销售订单信息", notes = "保存销售订单信息用户提交的订单信息" )
    public WebResult saveSaleOrder(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel){
        Integer result;
        try {
            result = orderService.saveSaleTask(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            }else if(result==-1){
                return fail("合同余额不足");
            }
            else if(result==0){
                return success("已创建任务无法调整");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
    @PostMapping("cancel/{sn}")
    @ApiOperation(value = "撤销销售订单信息", notes = "撤销订单信息" )
    public WebResult cancelSaleOrder(@PathVariable("sn")String sn){
        try{
            var result =orderService.cancelOrder(sn, SaleOrderState.Draft.code);
            if(result>0) {
                return success("撤销成功");
            }
            else{
                return fail("已生产不能撤销");
            }
        }
        catch (Exception ee){
            return error(ee);
        }
    }
}
